<script>
  $(document).ready(function(){
      //to retain the scroll position
      if (sessionStorage.scrollTop != "undefined") {
        $(window).scrollTop(sessionStorage.scrollTop);
      }

      //checking the active filters
      var activeFilters = $.getQueryParameters();
      var filters = document.getElementsByClassName('custom-control-input');
      for (var f=0; filters[f]; ++f){
          if (activeFilters.q[0].search(filters[f].value)!=-1)
              filters[f].checked = true;
      }
      document.getElementById("search-bar-input").value = '';
  
      //number of results found
      setTimeout(function() {
          if (!document.getElementsByClassName('result-card').length)
              {document.getElementById('search-results').innerHTML += '<h3>No tutorials found!</h3>';}
          else{
            document.getElementById('search-results').innerHTML = ('<h4>'+document.getElementsByClassName('result-card').length +' tutorials found</h4>') + document.getElementById('search-results').innerHTML;
          }
      },1000);
  });

  //update filters
  function applyFilter(){
      sessionStorage.scrollTop = $(window).scrollTop(); //to store the current scroll position
      var checkedValue = 'filterTutorials';    //because we only want to search the tutorials
      var inputElements = document.getElementsByClassName('custom-control-input');
      for(var i=0; inputElements[i]; ++i){
          if(inputElements[i].checked){
              checkedValue += '+' + inputElements[i].value ;   //new query
          }
      }
      var url = window.location.href.split("tutorials.html")[0] + "tutorials.html?q=" + checkedValue;
      window.location.href = url;
  }

  //search filter
  function filterFunction() {
        var input, filterQuery, filterDiv, filterDivLabel, ul, li, i;
        input = document.getElementById("filter-search");
        filterQuery = input.value.toUpperCase();
        filterDiv = document.getElementsByClassName("custom-control");
        filterDivLabel = document.getElementsByClassName("custom-control-label");
        for (i = 0; i < filterDiv.length; i++) {
          if (filterDivLabel[i].innerHTML.toUpperCase().indexOf(filterQuery) > -1) {
            filterDiv[i].style.display = "";
          } else {
            filterDiv[i].style.display = "none";
          }
        }

        var toggleCollapse, viewButton, j;
        toggleCollapse = document.getElementsByClassName("collapse");
        viewButton = document.getElementsByClassName("view-button");
        for(j = 0; j < toggleCollapse.length; j++){
          toggleCollapse[j].className = "collapse in";
          viewButton[j].children[0].style.display = "none";
          viewButton[j].children[1].style.display = "none";
        }

        if(filterQuery == ""){
          for(j = 0; j < toggleCollapse.length; j++){
            toggleCollapse[j].className = "collapse";
            viewButton[j].children[0].style.display = "";
            viewButton[j].children[1].style.display = "none";
          }
        }

      }
</script>

<form action="">
  <div class="form-group">
    <input type="text" name="q" id="filter-search" class="form-control" placeholder="Search for a filter" onkeyup="filterFunction()"/>
  </div>
  </form>

  <!-- Astropy Packages -->
  <h3 style="font-size: 1rem; font-weight: 500; text-decoration: none;">Astropy packages</h3>
  <form name="filterForm" class="form-group" style="padding-left: 20px; margin-bottom: 0;" action="">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck1" value="filterastroquery" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck1">astroquery</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck2" value="filtercoordinates" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck2">coordinates</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck3" value="filterastroplan" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck3">astroplan</label>
    </div>
    <!-- Display 3 in compact view -->
      <div class="collapse" id="viewmore1">
    <!-- -->
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck4" value="filterreproject" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck4">reproject</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck5" value="filtertable" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck5">table</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck6" value="filterspectralcube" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck6">spectral cube</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck7" value="filteraplpy" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck7">aplpy</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck8" value="filterimexam" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck8">imexam</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck9" value="filterspecutils" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck9">specutils</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck10" value="filterphotutils" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck10">photutils</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck11" value="filterwcs" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck11">WCS</label>
    </div>
        <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck12" value="filterunits" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck12">units</label>
    </div>
        <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck13" value="filtertime" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck13">time</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck15" value="filtersynphot" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck15">synphot</label>
    </div>
        <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck14" value="filterdustextinction" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck14">dust extinction</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck16" value="filtermodeling" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck16">modeling</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck17" value="filterconvolution" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck17">convolution</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck18" value="filtergala" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck18">gala</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck19" value="filtervoconesearch" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck19">vo conesearch</label>
    </div>


  </div>

  <a data-toggle="collapse" data-target="#viewmore1" aria-expanded="false" style="text-decoration: none !important" class="view-button">

    <span id="viewmore" style="text-decoration: none !important; color: #ff6a22 !important; font-size: 0.8rem;">
      VIEW MORE
    </span>
    <span id="viewless" style="text-decoration: none !important; color: #ff6a22 !important; font-size: 0.8rem;">
      VIEW LESS
    </span>

  </a>
  </form>

  <!-- Python Packages -->
  <h3 style="font-size: 1rem; font-weight: 500; text-decoration: none; padding-top: 15px;">Python packages</h3>
  <form  name="filterForm" class="form-group" style="padding-left: 20px; margin-bottom: 0;" action="">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck11" value="filtermatplotlib" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck11">matplotlib</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck20" value="filternumpy" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck20">numpy</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck21" value="filterscipy" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck21">scipy</label>
    </div>
  </form>

  <!-- Task -->
  <h3 style="font-size: 1rem; font-weight: 500; text-decoration: none; padding-top: 15px;">Task</h3>
  <form  name="filterForm" class="form-group" style="padding-left: 20px; margin-bottom: 0;" action="">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck22" value="filtercontourplots" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck22">contour plots</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck23" value="filterdatacubes" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck23">data cubes</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck24" value="filterfits" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck24">FITS</label>
    </div>
    <!-- -->
    <div class="collapse" id="viewmore2">
    <!-- -->
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck25" value="filterimageanalysis" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck25">image analysis</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck26" value="filterimagemanipulation" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck26">image manipulation</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck27" value="filtermodelfitting" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck27">model fitting</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck28" value="filterfileinputoutput" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck28">file input/output</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck29" value="filterhistogram" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck29">histogram</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck31" value="filtererrorbars " onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck30">error bars</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck32" value="filteroop" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck32">OOP</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck33" value="filterscatterplots" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck33">scatter plots</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck34" value="filterspectroscopy" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck34">spectroscopy</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck35" value="filtertimeseries" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck35">time series</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck36" value="filterunits" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck36">units</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck37" value="filtervizier" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck37">Vizier</label>
    </div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" name="q" class="custom-control-input" id="customCheck39" value="filterphotometry" onClick="applyFilter()">
        <label class="custom-control-label" for="customCheck39">photometry</label>
    </div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" name="q" class="custom-control-input" id="customCheck38" value="filtercolorbar" onClick="applyFilter()">
        <label class="custom-control-label" for="customCheck38">colorbar</label>
    </div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" name="q" class="custom-control-input" id="customCheck40" value="filterlatex" onClick="applyFilter()">
        <label class="custom-control-label" for="customCheck40">LaTeX</label>
    </div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" name="q" class="custom-control-input" id="customCheck41" value="filtermodularcode" onClick="applyFilter()">
        <label class="custom-control-label" for="customCheck41">modular code</label>
    </div>
    <div class="custom-control custom-checkbox">
        <input type="checkbox" name="q" class="custom-control-input" id="customCheck42" value="filtersimbad" onClick="applyFilter()">
        <label class="custom-control-label" for="customCheck42">Simbad</label>
    </div>

  </div>

  <a data-toggle="collapse" data-target="#viewmore2" aria-expanded="false" style="text-decoration: none !important" class="view-button">

    <span id="viewmore" style="text-decoration: none !important; color: #ff6a22 !important; font-size: 0.8rem;">
      VIEW MORE
    </span>
    <span id="viewless" style="text-decoration: none !important; color: #ff6a22 !important; font-size: 0.8rem;">
      VIEW LESS
    </span>

  </a>
  </form>

  <!-- Science -->
  <h3 style="font-size: 1rem; font-weight: 500; text-decoration: none; padding-top: 15px;">Science</h3>
  <form  name="filterForm" class="form-group" style="padding-left: 20px; margin-bottom: 0;" action="">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck44" value="filterastrodynamics" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck44">astrodynamics</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck43" value="filterstellarphotometry" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck43">stellar photometry</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck45" value="filterradioastronomy" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck45">radio astronomy</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck46" value="filterastrostatistics" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck46">astrostatistics</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck47" value="filterx-rayastronomy" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck47">x-ray astronomy</label>
    </div>
    <!-- -->
    <div class="collapse" id="viewmore3">
    <!-- -->
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck48" value="filtergalacticastronomy" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck48">galactic astronomy</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck49" value="filtergalaxydynamics" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck49">galaxy dynamics</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck50" value="filterstellarevolution" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck50">stellar evolution</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck51" value="filterphysics" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck51">physics</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck52" value="filterobservationalastronomy" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck52">observational astronomy</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck53" value="filterastrometry" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck53">astrometry</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck54" value="filterextinction" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck54">extinction</label>
    </div>
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="q" class="custom-control-input" id="customCheck55" value="filterpolarimetry" onClick="applyFilter()">
      <label class="custom-control-label" for="customCheck55">polarimetry</label>
    </div>

</div>

  <a data-toggle="collapse" data-target="#viewmore3" aria-expanded="false" style="text-decoration: none !important" class="view-button">

    <span id="viewmore" style="text-decoration: none !important; color: #ff6a22 !important; font-size: 0.8rem;">
      VIEW MORE
    </span>
    <span id="viewless" style="text-decoration: none !important; color: #ff6a22 !important; font-size: 0.8rem;">
      VIEW LESS
    </span>

  </a>
  </form>
